//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "ECB.h"
using namespace Cephei::QL::Times;
#undef HANDLE
#undef ABSTRACT
#define STRUCT
Cephei::QL::Times::CECB::CECB (boost::shared_ptr<QuantLib::ECB>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phECB = NULL;
#endif
	_ppECB = &childNative;
    
}
Cephei::QL::Times::CECB::CECB (QuantLib::ECB& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phECB = NULL;
#endif
	_ppECB = new boost::shared_ptr<QuantLib::ECB> (&childNative);
    
    _ECBOwner = owner;
    
}

Cephei::QL::Times::CECB::CECB (CECB^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phECB = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppECB = new boost::shared_ptr<QuantLib::ECB> (copy->GetShared());
        
    }
}
Cephei::QL::Times::CECB::CECB (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phECB = NULL;
#endif
	if (!t->IsSubclassOf(CECB::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Times::CECB::CECB (QuantLib::Handle<QuantLib::ECB>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phECB = &childNative;
	_ppECB = &static_cast<boost::shared_ptr<QuantLib::ECB>>(childNative.currentLink());
    
    _ECBOwner = owner;
}
Cephei::QL::Times::CECB::CECB (QuantLib::Handle<QuantLib::ECB> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phECB = &childNative;
	_ppECB = &static_cast<boost::shared_ptr<QuantLib::ECB>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Times::CECB::CECB (QuantLib::ECB childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phECB = NULL;
#endif
	_ppECB = new boost::shared_ptr<QuantLib::ECB> (new QuantLib::ECB (childNative));
    
}
#endif

Cephei::QL::Times::CECB::~CECB ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppECB != NULL)
    {
	    delete _ppECB;
        _ppECB = NULL;
    }
}
Cephei::QL::Times::CECB::!CECB ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppECB != NULL)
    {
	    delete _ppECB;
    }
}
QuantLib::ECB& Cephei::QL::Times::CECB::GetReference ()
{
    if (_ppECB == NULL) throw REFNEW NativeNullException ();
	return **_ppECB;
}
boost::shared_ptr<QuantLib::ECB>& Cephei::QL::Times::CECB::GetShared ()
{
    if (_ppECB == NULL) throw REFNEW NativeNullException ();
	return *_ppECB;
}
QuantLib::ECB* Cephei::QL::Times::CECB::GetPointer ()
{
    if (_ppECB == NULL) throw REFNEW NativeNullException ();
	return &**_ppECB;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ECB>& Cephei::QL::Times::CECB::GetHandle ()
{
	if (_phECB == NULL)
	{
		_phECB = new Handle<QuantLib::ECB> (*_ppECB);
	}
	return *_phECB;
}
#endif
bool Cephei::QL::Times::CECB::HasNative () 
{
	return (_ppECB != NULL);
}

Cephei::QL::Times::IECB^ Cephei::QL::Times::CECB::AddDate (DateTime d)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Date _d = (QuantLib::Date)ValueHelper::Convert (d); //a
    	(*_ppECB)->addDate ( _d );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
String^ Cephei::QL::Times::CECB::Code (DateTime ecbDate)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Date _ecbDate = (QuantLib::Date)ValueHelper::Convert (ecbDate); //a
    	std::string _rv = (std::string)(*_ppECB)->code ( _ecbDate );   
        String^ _nrv = (String^)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Times::CECB::Date (String^ ecbCode, Microsoft::FSharp::Core::FSharpOption<DateTime>^ referenceDate)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        std::string _ecbCode = (std::string)ValueHelper::Convert (ecbCode); //a
        QuantLib::Date _referenceDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (referenceDate) ? (QuantLib::Date)ValueHelper::Convert (referenceDate->Value) : QuantLib::Date()); //9a
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppECB)->date ( _ecbCode,  _referenceDate );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Times::CECB::IsECBcode (String^ p_in)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        std::string _p_in = (std::string)ValueHelper::Convert (p_in); //a
    	bool _rv = (bool)(*_ppECB)->isECBcode ( _p_in );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Times::CECB::IsECBdate (DateTime d)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Date _d = (QuantLib::Date)ValueHelper::Convert (d); //a
    	bool _rv = (bool)(*_ppECB)->isECBdate ( _d );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
String^ Cephei::QL::Times::CECB::NextCode (String^ ecbCode)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        std::string _ecbCode = (std::string)ValueHelper::Convert (ecbCode); //a
    	std::string _rv = (std::string)(*_ppECB)->nextCode ( _ecbCode );   
        String^ _nrv = (String^)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
String^ Cephei::QL::Times::CECB::NextCode (Microsoft::FSharp::Core::FSharpOption<DateTime>^ d)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Date _d = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (d) ? (QuantLib::Date)ValueHelper::Convert (d->Value) : QuantLib::Date()); //9a
    	std::string _rv = (std::string)(*_ppECB)->nextCode ( _d );   
        String^ _nrv = (String^)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Times::CECB::NextDate (String^ ecbCode, Microsoft::FSharp::Core::FSharpOption<DateTime>^ referenceDate)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        std::string _ecbCode = (std::string)ValueHelper::Convert (ecbCode); //a
        QuantLib::Date _referenceDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (referenceDate) ? (QuantLib::Date)ValueHelper::Convert (referenceDate->Value) : QuantLib::Date()); //9a
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppECB)->nextDate ( _ecbCode,  _referenceDate );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Times::CECB::NextDate (Microsoft::FSharp::Core::FSharpOption<DateTime>^ d)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Date _d = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (d) ? (QuantLib::Date)ValueHelper::Convert (d->Value) : QuantLib::Date()); //9a
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppECB)->nextDate ( _d );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<DateTime>^ Cephei::QL::Times::CECB::NextDates (String^ ecbCode, Microsoft::FSharp::Core::FSharpOption<DateTime>^ referenceDate)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        std::string _ecbCode = (std::string)ValueHelper::Convert (ecbCode); //a
        QuantLib::Date _referenceDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (referenceDate) ? (QuantLib::Date)ValueHelper::Convert (referenceDate->Value) : QuantLib::Date()); //9a
    	std::vector<QuantLib::Date> _rv = (std::vector<QuantLib::Date>)(*_ppECB)->nextDates ( _ecbCode,  _referenceDate );   
        Cephei::Core::IVector<DateTime>^ _nrv = REFNEW CoVector<DateTime> (REFNEW CDateTimeVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<DateTime>^ Cephei::QL::Times::CECB::NextDates (Microsoft::FSharp::Core::FSharpOption<DateTime>^ d)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Date _d = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (d) ? (QuantLib::Date)ValueHelper::Convert (d->Value) : QuantLib::Date()); //9a
    	std::vector<QuantLib::Date> _rv = (std::vector<QuantLib::Date>)(*_ppECB)->nextDates ( _d );   
        Cephei::Core::IVector<DateTime>^ _nrv = REFNEW CoVector<DateTime> (REFNEW CDateTimeVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IECB^ Cephei::QL::Times::CECB::RemoveDate (DateTime d)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Date _d = (QuantLib::Date)ValueHelper::Convert (d); //a
    	(*_ppECB)->removeDate ( _d );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

